package com.yaodian100.mall.supplierapply.web;

import java.io.File;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.aspectj.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.core.menu.service.MenuService;
import com.yaodian100.ec.category.entity.Category;
import com.yaodian100.ec.category.service.CategoryService;
import com.yaodian100.ec.supplierapply.entity.NewSupplierApply;
import com.yaodian100.ec.supplierapply.entity.SupplierApplyPhoto;
import com.yaodian100.ec.supplierapply.service.NewSupplierApplyService;

/**
 * @author Tina.xu
 * 
 */
public class NewSupplierApplyAction extends ActionSupport implements Preparable {

	@Autowired
	private NewSupplierApplyService newsupplierApplyService;
	@Autowired
	private CategoryService categoryService;
	@Autowired
	private MenuService menuService;
	private NewSupplierApply newsupplierapply;
	private List<Category> rootCategories;// 館分類
	private Menu menuTaxInvoice;// 納稅人性質/發票性質
	private String photoid;
	private String esyear;
	private String esmonth;
	private String estime;
	private String[] photoDelete;
	private File image;
	private File image1;
	private File image2;
	private File image3;
	private File image4;
	private String imageContentType;
	private String image1ContentType;
	private String image2ContentType;
	private String image3ContentType;
	private String image4ContentType;
	private String imageFileName;
	private String image1FileName;
	private String image2FileName;
	private String image3FileName;
	private String image4FileName;
	private String imageDecription;
	private String imageDecription1;
	private String imageDecription2;
	private String imageDecription3;
	private String imageDecription4;
	private String supplierID;
	private List<SupplierApplyPhoto> nasphotoList;
	private int fileSize = 1024;
	private String saveResult = "";

	public String add() {
		newsupplierapply = new NewSupplierApply();
		return "add";
	}

	public String view() {
		if (StringUtils.isNotEmpty(supplierID)) {
			newsupplierapply = newsupplierApplyService.get(supplierID);
			nasphotoList = newsupplierApplyService.getPhotos(supplierID);
		} else {
			newsupplierapply = null;
		}
		return "view";
	}

	public String save() {
		try {
			if (StringUtils.isNotBlank(esyear) && StringUtils.isNotBlank(esmonth)) {
				estime = esyear.trim() + "-" + esmonth.trim();
				newsupplierapply.setCompanyesttime(estime);
			}
			newsupplierapply = newsupplierApplyService.save(newsupplierapply);
			if (newsupplierapply != null) {
				supplierID = newsupplierapply.getSupplierID();
			}
			if (newsupplierapply.getSupplierID() != null) {
				try {
					if (image != null && imageDecription != null) {
						byte[] finp = FileUtil.readAsByteArray(image);
						newsupplierApplyService.saveforPhoto(newsupplierapply, finp, imageDecription, imageFileName);
					}

				} catch (Exception e) {
					addActionMessage("档案:「" + imageFileName + "」上传失败");
				}
				try {
					if (image1 != null && imageDecription1 != null) {
						byte[] finp = FileUtil.readAsByteArray(image1);
						newsupplierApplyService.saveforPhoto(newsupplierapply, finp, imageDecription1, image1FileName);
					}

				} catch (Exception e) {
					addActionMessage("档案:「" + image1FileName + "」上传失败");
				}
				try {
					if (image2 != null && imageDecription2 != null) {
						byte[] finp = FileUtil.readAsByteArray(image2);
						newsupplierApplyService.saveforPhoto(newsupplierapply, finp, imageDecription2, image2FileName);
					}
				} catch (Exception e) {
					addActionMessage("档案:「" + image2FileName + "」上传失败");
				}

				try {
					if (image3 != null && imageDecription3 != null) {
						byte[] finp = FileUtil.readAsByteArray(image3);
						newsupplierApplyService.saveforPhoto(newsupplierapply, finp, imageDecription3, image3FileName);
					}
				} catch (Exception e) {
					addActionMessage("档案:「" + image3FileName + "」上传失败");
				}

				try {
					if (image4 != null && imageDecription4 != null) {
						byte[] finp = FileUtil.readAsByteArray(image4);
						newsupplierApplyService.saveforPhoto(newsupplierapply, finp, imageDecription4, image4FileName);
					}
				} catch (Exception e) {
					addActionMessage("档案:「" + image4FileName + "」上传失败");
				}
			}

			return view();
		} catch (Exception e) {
			return view();
		}
	}

	@Override
	public void prepare() throws Exception {
		clearErrorsAndMessages();
		if (rootCategories == null) {
			rootCategories = categoryService.getChild(Long.parseLong("0"));
		}
		if (menuTaxInvoice == null) {
			menuTaxInvoice = menuService.get("Supplier.taxInvoice");
		}

	}

	public List<Category> getRootCategories() {
		return rootCategories;
	}

	public void setRootCategories(List<Category> rootCategories) {
		this.rootCategories = rootCategories;
	}

	public String getPhotoid() {
		return photoid;
	}

	public void setPhotoid(String photoid) {
		this.photoid = photoid;
	}

	public String getImageDdecription() {
		return imageDecription;
	}

	public void setImageDdecription(String imageDdecription) {
		this.imageDecription = imageDdecription;
	}

	public String getEsyear() {
		return esyear;
	}

	public void setEsyear(String esyear) {
		this.esyear = esyear;
	}

	public String getEsmonth() {
		return esmonth;
	}

	public void setEsmonth(String esmonth) {
		this.esmonth = esmonth;
	}

	public String getEstime() {
		return estime;
	}

	public void setEstime(String estime) {
		this.estime = estime;
	}

	public NewSupplierApply getNewsupplierapply() {
		return newsupplierapply;
	}

	public void setNewsupplierapply(NewSupplierApply newsupplierapply) {
		this.newsupplierapply = newsupplierapply;
	}

	public void setImage(File image) {
		this.image = image;
	}

	public void setImage1(File image1) {
		this.image1 = image1;
	}

	public String[] getPhotoDelete() {
		return photoDelete;
	}

	public void setPhotoDelete(String[] photoDelete) {
		this.photoDelete = photoDelete;
	}

	public void setImage2(File image2) {
		this.image2 = image2;
	}

	public void setImage3(File image3) {
		this.image3 = image3;
	}

	public void setImage4(File image4) {
		this.image4 = image4;
	}

	public void setImage1FileName(String image1FileName) {
		this.image1FileName = image1FileName;
	}

	public void setImage2FileName(String image2FileName) {
		this.image2FileName = image2FileName;
	}

	public void setImage3FileName(String image3FileName) {
		this.image3FileName = image3FileName;
	}

	public void setImage4FileName(String image4FileName) {
		this.image4FileName = image4FileName;
	}

	public void setImageContentType(String imageContentType) {
		this.imageContentType = imageContentType;
	}

	public void setImage1ContentType(String image1ContentType) {
		this.image1ContentType = image1ContentType;
	}

	public void setImage2ContentType(String image2ContentType) {
		this.image2ContentType = image2ContentType;
	}

	public void setImage3ContentType(String image3ContentType) {
		this.image3ContentType = image3ContentType;
	}

	public void setImage4ContentType(String image4ContentType) {
		this.image4ContentType = image4ContentType;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	public File getImage() {
		return image;
	}

	public File getImage1() {
		return image1;
	}

	public File getImage2() {
		return image2;
	}

	public File getImage3() {
		return image3;
	}

	public File getImage4() {
		return image4;
	}

	public String getImageContentType() {
		return imageContentType;
	}

	public String getImage1ContentType() {
		return image1ContentType;
	}

	public String getImage2ContentType() {
		return image2ContentType;
	}

	public String getImage3ContentType() {
		return image3ContentType;
	}

	public String getImage4ContentType() {
		return image4ContentType;
	}

	public String getImage1FileName() {
		return image1FileName;
	}

	public String getImage2FileName() {
		return image2FileName;
	}

	public String getImage3FileName() {
		return image3FileName;
	}

	public String getImage4FileName() {
		return image4FileName;
	}

	public String getImageDecription() {
		return imageDecription;
	}

	public void setImageDecription(String imageDecription) {
		this.imageDecription = imageDecription;
	}

	public String getImageDecription1() {
		return imageDecription1;
	}

	public void setImageDecription1(String imageDecription1) {
		this.imageDecription1 = imageDecription1;
	}

	public String getImageDecription2() {
		return imageDecription2;
	}

	public void setImageDecription2(String imageDecription2) {
		this.imageDecription2 = imageDecription2;
	}

	public String getImageDecription3() {
		return imageDecription3;
	}

	public void setImageDecription3(String imageDecription3) {
		this.imageDecription3 = imageDecription3;
	}

	public String getImageDecription4() {
		return imageDecription4;
	}

	public void setImageDecription4(String imageDecription4) {
		this.imageDecription4 = imageDecription4;
	}

	public int getFileSize() {
		return fileSize;
	}

	public void setFileSize(int fileSize) {
		this.fileSize = fileSize;
	}

	public String getSaveResult() {
		return saveResult;
	}

	public void setSaveResult(String saveResult) {
		this.saveResult = saveResult;
	}

	public Menu getMenuTaxInvoice() {
		return menuTaxInvoice;
	}

	public void setMenuTaxInvoice(Menu menuTaxInvoice) {
		this.menuTaxInvoice = menuTaxInvoice;
	}

	public List<SupplierApplyPhoto> getNasphotoList() {
		return nasphotoList;
	}

	public void setNasphotoList(List<SupplierApplyPhoto> nasphotoList) {
		this.nasphotoList = nasphotoList;
	}

}
